package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.PersonDao;
import com.nc.delivery_project.server.domain.LegalPerson;
import com.nc.delivery_project.server.domain.NaturalPerson;
import com.nc.delivery_project.server.domain.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by miroshnik on 1/28/2015.
 */
public class PersonService<T extends User> extends BaseService<T> {
    
    public PersonService(AbstractDao<T> dao, Connection connection, boolean isAutoCloseConnection) {
        super(dao, connection, isAutoCloseConnection);
    }

    public PersonService(AbstractDao<T> dao) {
        super(dao);
    }

    public PersonService(AbstractDao<T> dao, Connection connection) {
        super(dao, connection);
    }

    public PersonService(AbstractDao<T> dao, boolean isAutoCloseConnection) {
        super(dao, isAutoCloseConnection);
    }

    @Override
    public List<T> read() {
        List<T> persons = null;

        try {
            dao.setConnection(getConnection());
            persons = dao.read();
            for (T user : persons) {
                fillUserData(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return persons;
    }

    @Override
    public T read(int id) {
        T person = null;

        try {
            dao.setConnection(getConnection());
            person = dao.read(id);

            if (person != null) {
                fillUserData(person);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return person;
    }

    public T readPersonByUserId(int userId) {
        T person = null;
        try {
            dao.setConnection(getConnection());
            person = ((PersonDao<T>) dao).readByUserId(userId);

            if (person != null) {
                fillUserData(person);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return person;
    }

    private void fillUserData(T person) throws SQLException {
        User user = new UserService(getConnection(), false).read(person.getId());
        person.setEmail(user.getEmail());
        person.setRole(user.getRole());
        person.setPassword(user.getPassword());
    }
}
